{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "17a04a43-8e33-4ba6-a0bc-68d81f8a5bd5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "传递进来的--老马的程序人生--2--叫做实参，因为Ta是具体的参数值！\n",
      "函数中定义的doc内容\n",
      "Help on function MyFirstFunction in module __main__:\n",
      "\n",
      "MyFirstFunction(name, age)\n",
      "    函数中定义的doc内容\n",
      "\n"
     ]
    }
   ],
   "source": [
    "def MyFirstFunction(name, age):\n",
    "    \"函数中定义的doc内容\"\n",
    "    # 因为Ta只是一个形式，表示占据一个参数位置\n",
    "    print('传递进来的--{0}--{1}--叫做实参，因为Ta是具体的参数值！'.format(name, age))\n",
    "\n",
    "\n",
    "MyFirstFunction('老马的程序人生', '2')  \n",
    "# 传递进来的--老马的程序人生--2--叫做实参，因为Ta是具体的参数值！\n",
    "\n",
    "print(MyFirstFunction.__doc__)  \n",
    "# 函数中定义的doc内容\n",
    "\n",
    "help(MyFirstFunction)\n",
    "# Help on function MyFirstFunction in module __main__:\n",
    "# MyFirstFunction(name, age)\n",
    "#    函数中定义的doc内容"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "2ffed68b-1420-4f8c-9d95-b0ed6f3c05f0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Name:小马,Age:8\n",
      "Name:小马,Age:10\n"
     ]
    }
   ],
   "source": [
    "# 参数默认值\n",
    "def printinfo(name, age=8):\n",
    "    print('Name:{0},Age:{1}'.format(name, age))\n",
    "\n",
    "\n",
    "printinfo('小马')  # Name:小马,Age:8\n",
    "printinfo('小马', 10)  # Name:小马,Age:10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "95d8a975-2019-4081-ae6f-f102a4331587",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Name:小马,Age:8\n"
     ]
    }
   ],
   "source": [
    "# 打乱参数顺序\n",
    "def printinfo(name, age):\n",
    "    print('Name:{0},Age:{1}'.format(name, age))\n",
    "\n",
    "\n",
    "printinfo(age=8, name='小马')  # Name:小马,Age:8"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "e0698344-df4b-4f4e-a773-35015ac6f5e9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10\n",
      "70\n",
      "60 50 "
     ]
    }
   ],
   "source": [
    "# *可变参数，可以是从零个到任意个，自动组装成元组\n",
    "\n",
    "def printinfo(arg1, *args):\n",
    "    print(arg1)\n",
    "    for var in args:\n",
    "        print(var, end=\" \" )\n",
    "\n",
    "\n",
    "printinfo(10)  # 10\n",
    "printinfo(70, 60, 50)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "f46f5107-4e5c-44dc-943e-92e095a4a66e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "70\n",
      "(60, 50)\n",
      "{}\n",
      "70\n",
      "(60, 50)\n",
      "{'a': 1, 'b': 2}\n"
     ]
    }
   ],
   "source": [
    "# ** - 关键字参数，可以是从零个到任意个，自动组装成字典。\n",
    "\n",
    "def printinfo(arg1, *args, **kwargs):\n",
    "    print(arg1)\n",
    "    print(args)\n",
    "    print(kwargs)\n",
    "\n",
    "\n",
    "printinfo(70, 60, 50)\n",
    "# 70\n",
    "# (60, 50)\n",
    "# {}\n",
    "printinfo(70, 60, 50, a=1, b=2)\n",
    "# 70\n",
    "# (60, 50)\n",
    "# {'a': 1, 'b': 2}\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "a337bf28-7894-4b64-a7e1-5e85767ae994",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "70\n",
      "()\n",
      "10\n",
      "{'a': 1, 'b': 2}\n",
      "70\n",
      "(10,)\n",
      "20\n",
      "{'a': 1, 'b': 2}\n"
     ]
    },
    {
     "ename": "TypeError",
     "evalue": "printinfo() missing 1 required keyword-only argument: 'nkw'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[9], line 14\u001b[0m\n\u001b[0;32m      9\u001b[0m \u001b[38;5;66;03m# 70\u001b[39;00m\n\u001b[0;32m     10\u001b[0m \u001b[38;5;66;03m# 10\u001b[39;00m\n\u001b[0;32m     11\u001b[0m \u001b[38;5;66;03m# {'a': 1, 'b': 2}\u001b[39;00m\n\u001b[0;32m     12\u001b[0m printinfo(\u001b[38;5;241m70\u001b[39m, \u001b[38;5;241m10\u001b[39m, nkw\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m20\u001b[39m, a\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m1\u001b[39m, b\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m2\u001b[39m)\n\u001b[1;32m---> 14\u001b[0m \u001b[43mprintinfo\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m70\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m10\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43ma\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mb\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;241;43m2\u001b[39;49m\u001b[43m)\u001b[49m\n\u001b[0;32m     15\u001b[0m \u001b[38;5;66;03m# 没有传入命名关键字参数nkw \u001b[39;00m\n\u001b[0;32m     16\u001b[0m \u001b[38;5;66;03m# TypeError: printinfo() takes 1 positional argument but 2 were given\u001b[39;00m\n",
      "\u001b[1;31mTypeError\u001b[0m: printinfo() missing 1 required keyword-only argument: 'nkw'"
     ]
    }
   ],
   "source": [
    "def printinfo(arg1, *aaa, nkw, **kwargs):\n",
    "    print(arg1)\n",
    "    print(aaa)\n",
    "    print(nkw)\n",
    "    print(kwargs)\n",
    "\n",
    "\n",
    "printinfo(70, nkw=10, a=1, b=2)\n",
    "# 70\n",
    "# 10\n",
    "# {'a': 1, 'b': 2}\n",
    "printinfo(70, 10, nkw=20, a=1, b=2)\n",
    "\n",
    "printinfo(70, 10, a=1, b=2)\n",
    "# 没有传入命名关键字参数nkw \n",
    "# TypeError: printinfo() takes 1 positional argument but 2 were given"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "000a7059-3b51-4f3b-a44c-36e5eee54d7e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "[1, 2, 3, 4, 5, 6]\n"
     ]
    }
   ],
   "source": [
    "def add(a, b):\n",
    "    return a + b\n",
    "\n",
    "\n",
    "print(add(1, 2))  # 3\n",
    "print(add([1, 2, 3], [4, 5, 6]))  # [1, 2, 3, 4, 5, 6]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "924e468f-7902-4255-9015-d888d7ac4c1d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, '小马的程序人生', 3.14]\n"
     ]
    }
   ],
   "source": [
    "def back():\n",
    "    return [1, '小马的程序人生', 3.14]\n",
    "\n",
    "\n",
    "print(back())  # [1, '小马的程序人生', 3.14]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "8e09869f-b383-4c22-ba42-797648034802",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1, '小马的程序人生', 3.14)\n"
     ]
    }
   ],
   "source": [
    "def back():\n",
    "    return 1, '小马的程序人生', 3.14\n",
    "\n",
    "\n",
    "print(back())  # (1, '小马的程序人生', 3.14)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "671d294c-3070-4b90-b6b8-13dab93b31a9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello\n",
      "None\n",
      "<class 'NoneType'>\n"
     ]
    }
   ],
   "source": [
    "def printme(str):\n",
    "    print(str)\n",
    "\n",
    "temp = printme('hello') # hello\n",
    "print(temp) # None\n",
    "print(type(temp))  # <class 'NoneType'>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "be67b0ef-1221-4638-9c41-fa4392204469",
   "metadata": {},
   "source": [
    "### 变量作用域\n",
    "- Python 中，程序的变量并不是在哪个位置都可以访问的，访问权限决定于这个变量是在哪里赋值的。\n",
    "- 定义在函数内部的变量拥有局部作用域，该变量称为局部变量。\n",
    "- 定义在函数外部的变量拥有全局作用域，该变量称为全局变量。\n",
    "- 局部变量只能在其被声明的函数内部访问，而全局变量可以在整个程序范围内访问。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "29ad0770-2d6b-40db-98cf-22fcd0f63e43",
   "metadata": {},
   "outputs": [
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "请输入原价: 95\n",
      "请输入折扣率: 0.9\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "打折后价格是:85.50\n"
     ]
    }
   ],
   "source": [
    "def discounts(price, rate):\n",
    "    final_price = price * rate\n",
    "    return final_price\n",
    "\n",
    "\n",
    "old_price = float(input('请输入原价:'))  # 98\n",
    "rate = float(input('请输入折扣率:'))  # 0.9\n",
    "new_price = discounts(old_price, rate)\n",
    "print('打折后价格是:%.2f' % new_price)  # 88.20"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1d69d9d1-9d0a-4df5-941a-7e97635aba56",
   "metadata": {},
   "source": [
    "当内部作用域想修改外部作用域的变量时，就要用到global和nonlocal关键字了。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "0a9f3764-22f6-4ccb-81cf-4fcd8a73f01a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "123\n",
      "123\n"
     ]
    }
   ],
   "source": [
    "num = 1\n",
    "\n",
    "def fun1():\n",
    "    global num  # 需要使用 global 关键字声明\n",
    "    print(num)  # 1\n",
    "    num = 123\n",
    "    print(num)  # 123\n",
    "\n",
    "\n",
    "fun1()\n",
    "print(num)  # 123"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "73deb45b-9217-47a9-ad2a-3d4642edcc33",
   "metadata": {},
   "source": [
    "内嵌函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "a8086d98-fd39-4faa-b8f8-61bbc7499014",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "outer函数在这被调用\n",
      "inner函数在这被调用\n"
     ]
    }
   ],
   "source": [
    "\n",
    "def outer():\n",
    "    print('outer函数在这被调用')\n",
    "\n",
    "    def inner():\n",
    "        print('inner函数在这被调用')\n",
    "\n",
    "    inner()  # 该函数只能在outer函数内部被调用\n",
    "\n",
    "\n",
    "outer()\n",
    "# outer函数在这被调用\n",
    "# inner函数在这被调用"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "651048d8-a297-4e0c-b3a6-4d80ee6337d1",
   "metadata": {},
   "source": [
    "### 闭包\n",
    "\n",
    "- 是函数式编程的一个重要的语法结构，是一种特殊的内嵌函数。\n",
    "- 如果在一个内部函数里对外层非全局作用域的变量进行引用，那么内部函数就被认为是闭包。\n",
    "- 通过闭包可以访问外层非全局作用域的变量，这个作用域称为 闭包作用域。\n",
    "- 闭包的返回值通常是函数。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "7385d3ae-0508-450f-b96b-dc18c94eca45",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'function'>\n",
      "40\n"
     ]
    }
   ],
   "source": [
    "def funX(x):\n",
    "    def funY(y):\n",
    "        return x * y\n",
    "\n",
    "    return funY\n",
    "\n",
    "\n",
    "i = funX(8)\n",
    "print(type(i))  # <class 'function'>\n",
    "print(i(5))  # 40"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "19af1dba-ee27-4f3b-872e-230764f71da2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "2\n",
      "0\n"
     ]
    }
   ],
   "source": [
    "def make_counter(init):\n",
    "    counter = [init]\n",
    "\n",
    "    def inc(): counter[0] += 1\n",
    "\n",
    "    def dec(): counter[0] -= 1\n",
    "\n",
    "    def get(): return counter[0]\n",
    "\n",
    "    def reset(): counter[0] = init\n",
    "\n",
    "    return inc, dec, get, reset\n",
    "\n",
    "\n",
    "inc, dec, get, reset = make_counter(0)\n",
    "inc()\n",
    "inc()\n",
    "inc()\n",
    "print(get())  # 3\n",
    "dec()\n",
    "print(get())  # 2\n",
    "reset()\n",
    "print(get())  # 0"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6cf26681-c6ae-4d76-984a-8a65afb8b585",
   "metadata": {},
   "source": [
    "- 如果要修改闭包作用域中的变量则需要 nonlocal 关键字"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "0d712f20-983c-4216-9708-a5fecb609251",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "100\n",
      "100\n"
     ]
    }
   ],
   "source": [
    "def outer():\n",
    "    num = 10\n",
    "\n",
    "    def inner():\n",
    "        nonlocal num  # nonlocal关键字声明\n",
    "        num = 100\n",
    "        print(num)\n",
    "\n",
    "    inner()\n",
    "    print(num)\n",
    "\n",
    "\n",
    "outer()\n",
    "\n",
    "# 100\n",
    "# 100"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e4391556-594e-4dff-b007-9a00c48c4cfd",
   "metadata": {},
   "source": [
    "### 递归\n",
    "\n",
    "- 如果一个函数在内部调用自身本身，这个函数就是递归函数。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "b910069f-01f8-4604-acc9-41ac160bf045",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "120\n",
      "120\n"
     ]
    }
   ],
   "source": [
    "# 利用循环\n",
    "n = 5\n",
    "for k in range(1, 5):\n",
    "    n = n * k\n",
    "print(n)  # 120\n",
    "\n",
    "# 利用递归\n",
    "def factorial(n):\n",
    "    if n == 1:\n",
    "        return 1\n",
    "    return n * factorial(n - 1)\n",
    "\n",
    "\n",
    "print(factorial(5)) # 120"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "29b981cd-339f-448d-aca4-a120cea07a31",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55]\n",
      "[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55]\n"
     ]
    }
   ],
   "source": [
    "# 斐波那契数列 f(n)=f(n-1)+f(n-2), f(0)=0 f(1)=1\n",
    "# 利用循环\n",
    "i = 0\n",
    "j = 1\n",
    "lst = list([i, j])\n",
    "for k in range(2, 11):\n",
    "    k = i + j\n",
    "    lst.append(k)\n",
    "    i = j\n",
    "    j = k\n",
    "print(lst)  \n",
    "# [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55]\n",
    "\n",
    "# 利用递归\n",
    "def recur_fibo(n):\n",
    "    if n <= 1:\n",
    "        return n\n",
    "    return recur_fibo(n - 1) + recur_fibo(n - 2)\n",
    "\n",
    "\n",
    "lst = list()\n",
    "for k in range(11):\n",
    "    lst.append(recur_fibo(k))\n",
    "print(lst)  \n",
    "# [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "bc839092-2642-432b-9425-f16403742fd8",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 设置递归的层数，Python默认递归层数为 100\n",
    "\n",
    "import sys\n",
    "\n",
    "sys.setrecursionlimit(1000)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "67798d8b-5dd9-4dab-bcd7-8cef41d3b933",
   "metadata": {},
   "source": [
    "## Lambda 表达式\n",
    "### 匿名函数的定义\n",
    "Python 使用 lambda 关键词来创建匿名函数，而非def关键词，它没有函数名，其语法结构如下：\n",
    "\n",
    ">lambda argument_list: expression\n",
    "\n",
    "- lambda - 定义匿名函数的关键词。\n",
    "- argument_list - 函数参数，它们可以是位置参数、默认参数、关键字参数，和正规函数里的参数类型一样。\n",
    "- :- 冒号，在函数参数和表达式中间要加个冒号。\n",
    "- expression - 只是一个表达式，输入函数参数，输出一些值。\n",
    "注意：\n",
    "\n",
    "- expression 中没有 return 语句，因为 lambda 不需要它来返回，表达式本身结果就是返回值。\n",
    "- 匿名函数拥有自己的命名空间，且不能访问自己参数列表之外或全局命名空间里的参数。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "7ae0a75b-e914-4809-8593-6d2a4c20b241",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<function sqr at 0x00000221DAD1B1A0>\n",
      "[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]\n",
      "<function <lambda> at 0x00000221DAD74220>\n",
      "[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]\n",
      "30\n",
      "15\n"
     ]
    }
   ],
   "source": [
    "def sqr(x):\n",
    "    return x ** 2\n",
    "\n",
    "\n",
    "print(sqr)\n",
    "# <function sqr at 0x000000BABD3A4400>\n",
    "\n",
    "y = [sqr(x) for x in range(10)]\n",
    "print(y)\n",
    "# [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]\n",
    "\n",
    "lbd_sqr = lambda x: x ** 2\n",
    "print(lbd_sqr)\n",
    "# <function <lambda> at 0x000000BABB6AC1E0>\n",
    "\n",
    "y = [lbd_sqr(x) for x in range(10)]\n",
    "print(y)\n",
    "# [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]\n",
    "\n",
    "\n",
    "sumary = lambda arg1, arg2: arg1 + arg2\n",
    "print(sumary(10, 20))  # 30\n",
    "\n",
    "func = lambda *args: sum(args)\n",
    "print(func(1, 2, 3, 4, 5))  # 15"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "205a3e98-28c2-4012-b7b5-726a0b964c60",
   "metadata": {},
   "source": [
    "### 匿名函数的应用\n",
    "函数式编程 是指代码中每一块都是不可变的，都由纯函数的形式组成。这里的纯函数，是指函数本身相互独立、互不影响，对于相同的输入，总会有相同的输出，没有任何副作用。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "97302764-314a-455c-a9a6-e969d00e74e1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[11, 12, 13]\n"
     ]
    }
   ],
   "source": [
    "# 非函数式编程\n",
    "def f(x):\n",
    "    for i in range(0, len(x)):\n",
    "        x[i] += 10\n",
    "    return x\n",
    "\n",
    "\n",
    "x = [1, 2, 3]\n",
    "f(x)\n",
    "print(x)\n",
    "# [11, 12, 13]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "a84c7e10-d0b2-499f-a26e-942f6369aeba",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[11, 12, 13]\n"
     ]
    }
   ],
   "source": [
    "#函数式编程\n",
    "def f(x):\n",
    "    y = []\n",
    "    for item in x:\n",
    "        y.append(item + 10)\n",
    "    return y\n",
    "\n",
    "\n",
    "x = [1, 2, 3]\n",
    "y = f(x)\n",
    "print(y)\n",
    "# [11, 12, 13]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "777f1cae-4af6-4ad3-a9ae-8e633aecc89e",
   "metadata": {},
   "source": [
    "匿名函数 常常应用于函数式编程的高阶函数 (high-order function)中，主要有两种形式：\n",
    "\n",
    "- 参数是函数 (filter, map)\n",
    "- 返回值是函数 (closure)\n",
    "\n",
    "如，在 filter和map函数中的应用：\n",
    "\n",
    "filter(function, iterable) 过滤序列，过滤掉不符合条件的元素，返回一个迭代器对象，如果要转换为列表，可以使用 list() 来转换。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "a485ce0b-7d46-44ce-9d97-519ff075088c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 3, 5, 7, 9]\n"
     ]
    }
   ],
   "source": [
    "odd = lambda x: x % 2 == 1\n",
    "templist = filter(odd, [1, 2, 3, 4, 5, 6, 7, 8, 9])\n",
    "print(list(templist))  # [1, 3, 5, 7, 9]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4781d19b-fcad-4c05-ba66-8bf45c23d5ee",
   "metadata": {},
   "source": [
    "- map(function, *iterables) 根据提供的函数对指定序列做映射。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "ba9e3b9c-473d-4aca-9921-e15928a0683c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 4, 9, 16, 25]\n",
      "[3, 7, 11, 15, 19]\n"
     ]
    }
   ],
   "source": [
    "m1 = map(lambda x: x ** 2, [1, 2, 3, 4, 5])\n",
    "print(list(m1))  \n",
    "# [1, 4, 9, 16, 25]\n",
    "\n",
    "m2 = map(lambda x, y: x + y, [1, 3, 5, 7, 9], [2, 4, 6, 8, 10])\n",
    "print(list(m2))  \n",
    "# [3, 7, 11, 15, 19]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "ca73557c-221d-4bd0-8b01-df8ce393004b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "15\n",
      "5\n",
      "3.0\n"
     ]
    }
   ],
   "source": [
    "# 除了 Python 这些内置函数，我们也可以自己定义高阶函数。\n",
    "def apply_to_list(fun, some_list):\n",
    "    return fun(some_list)\n",
    "\n",
    "lst = [1, 2, 3, 4, 5]\n",
    "print(apply_to_list(sum, lst))\n",
    "# 15\n",
    "\n",
    "print(apply_to_list(len, lst))\n",
    "# 5\n",
    "\n",
    "print(apply_to_list(lambda x: sum(x) / len(x), lst))\n",
    "# 3.0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bb04d9b2-dee9-40cd-b9eb-7dd137d5f8ec",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
